package com.jbt.mds.obdsdk.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.jbt.mds.obdsdk.R
import com.jbt.mds.obdsdk.model.VehicleHistory

class VehicleHistoryAdapter(
    private var data: List<VehicleHistory>,
    private val onFailReasonClick: (String) -> Unit
) : RecyclerView.Adapter<VehicleHistoryAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val tvPlate: TextView = view.findViewById(R.id.tv_plate)
        val tvVin: TextView = view.findViewById(R.id.tv_vin)
        val tvTime: TextView = view.findViewById(R.id.tv_time)
        val tvType: TextView = view.findViewById(R.id.tv_type)
        val tvStatus: TextView = view.findViewById(R.id.tv_status)
//        val tvFail: TextView = view.findViewById(R.id.tv_fail)  //需要开不合格项目具体的值，点击不合格项，就在这把注解打开
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_history, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int = data.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = data[position]
        holder.tvPlate.text = item.plate
        holder.tvVin.text = item.vin
        holder.tvTime.text = "检查时间：${item.time}"
        holder.tvType.text = item.vehicleType
        holder.tvStatus.text = item.status

        if (item.status == "合格") {
            holder.tvStatus.setTextColor(0xFF2196F3.toInt())
            holder.tvStatus.setBackgroundResource(R.drawable.bg_status_fail)
//            holder.tvFail.visibility = View.GONE
//            holder.tvFail.setOnClickListener(null) // 清除监听器，防止复用出错
        } else { // 不合格
            holder.tvStatus.setTextColor(0xFFFF9800.toInt())
            holder.tvStatus.setBackgroundResource(R.drawable.bg_status)
//            holder.tvFail.visibility = View.VISIBLE
//            // 设置点击事件
//            holder.tvFail.setOnClickListener {
//                onFailReasonClick(item.executeResult)
//            }
        }
    }

    fun updateData(newData: List<VehicleHistory>) {
        data = newData
        notifyDataSetChanged()
    }
}

